/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import DAO.UserDAO;
import DAO.EventDAO;
import connection.ApiProxyClient;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.UserBean;
import model.EventBean;
import model.UstreamBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author ozgencetinkaya
 */

@Controller
public class EventController {
    
    @RequestMapping(value="/event.htm")
    public String showEvent(Map model, @RequestParam(value="eventId", required=true) int eventId,HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException{
        
        Enumeration enames;
        HttpSession session = request.getSession(true);
        enames = session.getAttributeNames();

        if (!enames.hasMoreElements()) {
            response.sendRedirect("signin.htm");
        }
        else{
            UserBean currentUser = (UserBean)(session.getAttribute("currentSessionUser"));
            if(currentUser.isValid())  {
                
                EventDAO eventDAO = new EventDAO();
                EventBean eventBean = new EventBean();
                String ustreamUserName = eventDAO.getUstreamUserNameOfCreatorOfEvent(eventId);
                //ustreamUserName = "ozgencetinkaya";
                ApiProxyClient apc = new ApiProxyClient(ustreamUserName);
                UstreamBean ustreamBean = apc.getInfo();
                
                eventBean = eventDAO.getEventData(eventId);
                
                model.put("nameOfEvent",eventBean.getName());
                model.put("locationOfEvent",eventBean.getLocation());
                
                model.put("UstreamUserName", ustreamUserName);
                model.put("UID", ustreamBean.getResults().getId());
                
                return "event";
            }
            else
                response.sendRedirect("signin.htm");
        }
        return"event.htm";
    }
}
